<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class UserCategory_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $category_id
     * @return mixed
     * 查询单条
     */
    public function get_user_categorys_by_id($category_id){

        $this->db->select('*');
        $this->db->from('user_categorys');
        $this->db->where('category_id',$category_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $category_id
     * @param $data
     * @return bool
     * 修改喜欢频道信息
     */
    public function sava_user_categorys($category_id,$data){

        $this->db->where('category_id',$category_id);
        $this->db->update('user_categorys', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增喜欢频道
     */
    public function add_user_categorys($data){

        $this->db->insert('user_categorys',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有喜欢频道
     */
    public function get_all_user_categorys(){
        $this->db->select('*');
        $this->db->from('user_categorys');
        $this->db->join('shop_users','user_categorys.shop_id = shop_users.shop_id','left');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除喜欢频道
     */
    public function delete_user_categorys_by($where){
        $this->db->where($where)->delete('user_categorys');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function is_favorites($user_id,$category_tid)
    {
        $query = $this->db->select('*')->from('user_categorys')->where(array('user_id' => $user_id, 'category_tid' => $category_tid))->get();
        return $query->row_array();
    }

    /** 按照频道查询次数排序
     * @return mixed
     */
    public function get_hot_categorys(){
        $this->db->select('category_id, count(1) as num,fox_category.category_name,fox_category.id');
        $this->db->from('user_categorys');
        $this->db->join('fox_category','user_categorys.category_id = fox_category.id','left');
        $this->db->group_by("category_tid");
        $this->db->order_by('num','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
}
